package com.example.haojiutao.service.impl;


import com.example.haojiutao.common.Result;
import com.example.haojiutao.mapper.GoodsMapper;
import com.example.haojiutao.mapper.OrderMapper;
import com.example.haojiutao.model.domain.Order;
import com.example.haojiutao.model.vo.response.CommentOrderDetailVO;
import com.example.haojiutao.model.vo.response.OrderGoodsDetailVO;
import com.example.haojiutao.service.OrderService;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Description: 订单业务层实现类
 * @Author: zhou_yuan
 * @CreateTime： 2024/1/22 14:20
 */
@Service
@Slf4j
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private GoodsMapper goodsMapper;


    /*
     * @Description: 下单
     * @Author: zhou_yuan
     * @CreateTime： 2024/1/22 14:21
     * @param: goods
     * @return: com.example.haojiutao.common.Result
     */
    @Override
    public Result insertOrder(Order order) {
//        Order order =new Order();
        Integer goodsId = order.getGoodsId();
//        System.out.println("--------------------"+goodsId);
        //TODO 获取当前登录用户名称
        String userName = order.getUserName();
        String goodsName = order.getGoodsName();
        String sellerName = order.getSellerName();
        String trackingNumber = order.getTrackingNumber();
        String orderState = order.getOrderState();
        Integer amount = order.getAmount();
        Double orderPrice = order.getOrderPrice();
        Date orderTime = order.getOrderTime();
        Date receivingTime = order.getReceivingTime();
        String phoneNum = order.getPhoneNum();
        String address = order.getAddress();
        String notes = order.getNotes();
        String receivingState = order.getReceivingState();
        log.info("下单成功");
        order.setOrderTime(new Date());
        int rows = orderMapper.insertOrder(order);
        if (rows != 0) {
            return Result.success("下单成功！");
        }
        return Result.faile("下单失败");
    }


    /*
     * @Description:用户添加收货状态
     * @Author: zhou_yuan
     * @CreateTime： 2024/1/22 14:25
     * @param: order
     * @return: com.example.haojiutao.common.Result
     */
    @Override
    public Result updateOrder(Order order) {
        log.info("更新订单信息:order{}", order);
        order.setReceivingState("已签收");
        order.setOrderState("已收货");
        order.setReceivingTime(new Date());
        int rows = orderMapper.updateOrder(order);
        if (rows != 0) {
            return Result.success("修改成功！");

        }
        return Result.faile("修改失败");

    }


    /*
     * @Description:商家添加物流编号
     * @Author: zhou_yuan
     * @CreateTime： 2024/1/22 14:56
     * @param: order
     * @return: com.example.haojiutao.common.Result
     */
    @Override
    public Result updateOrderLogistics(Order order) {
        log.info("更新订单信息:order{}", order);
        order.setOrderState("已发货");
        int rows = orderMapper.updateOrder(order);
        if (rows != 0) {
            return Result.success("修改成功！");

        }
        return Result.faile("修改失败");

    }

    @Override
    public List<OrderGoodsDetailVO> selectListByUser(OrderGoodsDetailVO orderGoodsDetailVO) {
        PageHelper.startPage(orderGoodsDetailVO.getPageNum(), orderGoodsDetailVO.getPageSize());
        List<OrderGoodsDetailVO> order1 = orderMapper.selectListByUser(orderGoodsDetailVO);
        return order1;
    }

    /*
     * @Description:查看订单
     * @Author: zhou_yuan
     * @CreateTime： 2024/1/22 15:04
     * @param: orderNumber
     * @return: com.example.haojiutao.common.Result
     */
    @Override
    public Order selectById(Integer orderNumber) {
        Order order = orderMapper.selectById(orderNumber);
        log.info("order={}" + order);
        return order;
    }

    @Override
    public OrderGoodsDetailVO selectByOrderNumber(Integer orderNumber) {
        OrderGoodsDetailVO order = orderMapper.selectByOrderNumber(orderNumber);
        log.info("order={}" + order);
        return order;
    }


    /*
     * @Description: 显示个人订单列表
     * @Author: zhou_yuan
     * @CreateTime： 2024/1/22 15:10
     * @param: goods
     * @return: com.example.haojiutao.common.Result
     */
    @Override
    public List<Order> selectList(Order order) {
        PageHelper.startPage(order.getPageNum(), order.getPageSize());
        List<Order> order1 = orderMapper.selectList(order);
        return order1;
    }

    /*
     * @Description: TODO 监听订单是否超时？
     * @Author: zhou_yuan
     * @CreateTime： 2024/1/22 15:11
     * @param: orderNumber
     * @return: com.example.haojiutao.common.Result
     **/
    @Override
    public Result deleteById(Integer orderNumber) {
        OrderGoodsDetailVO order = new OrderGoodsDetailVO();
        order.setOrderNumber(orderNumber);
        order.setDelFlag(1);
        order.setDelTime(new Date());
        int rows = orderMapper.updateDeleteFlagById(order);
        if (rows == 1) {
            return Result.success("删除成功！");
        }
        return Result.faile("删除失败");
    }

    /*
     * @Description: 待发货订单列表
     * @Author: zhou_yuan
     * @CreateTime： 2024/1/22 15:13
     * @param: goods
     * @return: com.example.haojiutao.common.Result
     */
    @Override
    public List<OrderGoodsDetailVO> selectBackOrder(OrderGoodsDetailVO orderGoodsDetailVO) {
        PageHelper.startPage(orderGoodsDetailVO.getPageNum(), orderGoodsDetailVO.getPageSize());
        List<OrderGoodsDetailVO> order1 = orderMapper.selectBackOrder(orderGoodsDetailVO);
        return order1;
    }


    /*
     * @Description: 待收货订单列表
     * @Author: zhou_yuan
     * @CreateTime： 2024/1/22 15:24
     * @param: goods
     * @return: com.example.haojiutao.common.Result
     */
    @Override
    public List<OrderGoodsDetailVO> selectAfterSendOrder(OrderGoodsDetailVO orderGoodsDetailVO) {
        PageHelper.startPage(orderGoodsDetailVO.getPageNum(), orderGoodsDetailVO.getPageSize());
        List<OrderGoodsDetailVO> order1 = orderMapper.selectAfterSendOrder(orderGoodsDetailVO);
        return order1;
    }

    @Override
    public List<OrderGoodsDetailVO> selectBeforePayOrder(OrderGoodsDetailVO orderGoodsDetailVO) {
        PageHelper.startPage(orderGoodsDetailVO.getPageNum(), orderGoodsDetailVO.getPageSize());
        List<OrderGoodsDetailVO> order1 = orderMapper.selectBeforePayOrder(orderGoodsDetailVO);
        return order1;
    }



    /*
     * @Description: TODO 管理员修改订单信息
     * @Author: zhou_yuan
     * @CreateTime： 2024/1/22 15:26
     * @param: order
     * @return: com.example.haojiutao.common.Result
     **/
    @Override
    public Result saveOrder(Order order) {
        int rows = orderMapper.update(order);
        if (rows == 1) {
            return Result.success("保存成功");
        }
        return Result.faile("保存失败");


    }

    @Override
    public Result updateOrderComment(Integer orderNumber) {
        Order order = new Order();
        log.info("更新订单信息:order{}", order);
        order.setOrderNumber(orderNumber);
        order.setCommentFlag(1);
        Integer rows = orderMapper.updateOrderComment(order);
        if (rows != 0) {
            return Result.success("修改成功！");

        }
        return Result.faile("修改失败");
    }

}
